package com.newbingo.dao;

import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Criteria;
import org.hibernate.criterion.CriteriaSpecification;
import org.hibernate.criterion.Expression;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projections;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.util.StringUtils;

import com.newbingo.dao.imple.IGameRebateDao;
import com.newbingo.entity.GameRebate;
import com.newbingo.util.Pager;

public class GameRebateDao extends HibernateDaoSupport implements
		IGameRebateDao {
	private static final Log log = LogFactory.getLog(GameRebateDao.class);

	/*
	 * 删除(non-Javadoc)
	 * @see com.newbingo.dao.imple.IGameRebateDao#delete(com.newbingo.entity.GameRebate)
	 */
	public void delete(GameRebate rebate) {
		log.debug("deleting GameRebate Instance");
		try {
			GameRebate instance = this.findById(rebate.getId());
			getSession().delete(instance);
			log.debug("delete GameRebate instance successful");
		} catch (RuntimeException re) {
			log.error("delete GameRebate failed", re);
			throw re;
		}

	}

	/*
	 * 根据ID查询返回GameRebate对象(non-Javadoc)
	 * @see com.newbingo.dao.imple.IGameRebateDao#findById(java.lang.Integer)
	 */
	public GameRebate findById(Integer Id) {
		log.debug("finding GameRebate instance with:" + Id);
		try {
			GameRebate instance = (GameRebate) getSession().get(
					"com.newbingo.entity.GameRebate", Id);
			return instance;
		} catch (RuntimeException re) {
			log.error("find GameRebate failed", re);
			throw re;
		}
	}

	/*
	 * 分页查询 返回Pager对象(non-Javadoc)
	 * @see com.newbingo.dao.imple.IGameRebateDao#findRebate(int, int, java.lang.Integer, java.lang.String)
	 */
	public Pager findRebate(int start, int limit, Integer agentId,
			String gameName) {
		Pager pager = new Pager();
		Criteria cri = this.getSession().createCriteria(GameRebate.class);
		cri.createAlias("agent", "ag");
		cri.add(Expression.eq("ag.uno", agentId));
		cri.createAlias("game", "gm");
		if (gameName != null && StringUtils.hasText(gameName)) {
			cri.add(Expression.eq("gm.gamename", gameName));
		}
		cri.setProjection(Projections.rowCount());
		pager.setTotalSize(((Number) cri.uniqueResult()).intValue());
		cri.setProjection(null);
		cri.setFirstResult(start);
		cri.setMaxResults(limit);
		cri.setResultTransformer(Criteria.ROOT_ENTITY);
		pager.setData(cri.list());
		return pager;
	}

	/*
	 * 修改(non-Javadoc)
	 * @see com.newbingo.dao.imple.IGameRebateDao#merger(com.newbingo.entity.GameRebate)
	 */
	public GameRebate merger(GameRebate rebate) {
		log.debug("merging GameRebate instance");
		try {
			GameRebate result = (GameRebate) getSession().merge(rebate);
			return result;
		} catch (RuntimeException re) {
			log.error("merge GameRebate failed", re);
			throw re;
		}
	}

	/*
	 * 保存(non-Javadoc)
	 * @see com.newbingo.dao.imple.IGameRebateDao#save(com.newbingo.entity.GameRebate)
	 */
	public void save(GameRebate rebate) {
		log.debug("saving GameRebate instance");
		try {
			getSession().save(rebate);
		} catch (RuntimeException re) {
			log.error("save GameRebate failed", re);
			throw re;
		}

	}
	
	/*
	 * 根据代理商ID获取最近的游戏返利设置
	 */
	public GameRebate findLastestRebateByAgent(Integer agentId){
		log.debug("finding lastest GameRebate by Agent");
		try{
			Criteria cri=getSession().createCriteria(GameRebate.class);
			cri.createAlias("agent","agent");
			cri.add(Expression.eq("agent.uno", agentId));
			//ID倒叙，查询的是最新的游戏返利设置
			cri.addOrder(Order.desc("id"));
			cri.setResultTransformer(Criteria.ROOT_ENTITY);
			List<GameRebate> result=cri.list();
			if(result.size()>0)
				return result.get(0);
			else
				return null;
		}catch(RuntimeException re){
			log.error("find failed");
			throw re;
		}
	}

}
